using System;
using System.IO;
using System.Windows.Forms;

namespace Glpt.Common
{
    /// <summary>
    /// 文件选择控件
    /// </summary>
    public partial class FileSelect : UserControl
    {
        /// <summary>
        /// 获取或设置一个包含在文件对话框中选定的文件名的字符串
        /// </summary>
        public string FileName = "";
        /// <summary>
        /// 字节流
        /// </summary>
        public byte[] Buffer = new byte[1];
        /// <summary>
        /// 文件名关联控件
        /// </summary>
        public Control FileNameControl = null;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public FileSelect()
        {
            InitializeComponent();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog().Equals(DialogResult.OK))
                {
                    FileName = openFileDialog1.FileName;
                    txtFileName.Text = FileName;
                    if (FileNameControl != null)
                    {
                        FileInfo file = new FileInfo(FileName);
                        FileNameControl.Text = file.Name;
                    }
                }
            }
            catch (Exception ex)
            {
                Funcs.ShowError(ex);
            }
        }

        /// <summary>
        /// 从文件流读取字节块
        /// </summary>
        /// <returns>字节块</returns>
        public byte[] ReadByte()
        {
            if (FileName.Length > 0)
            {
                Buffer = new byte[1];
                FileInfo EleFile = new FileInfo(FileName);
                if (EleFile.Exists)
                {
                    Buffer = new byte[EleFile.Length];
                    FileStream fs = EleFile.OpenRead();
                    fs.Read(Buffer, 0, Buffer.Length);
                    fs.Close();
                }
            }
            if (Buffer.Length < 1) Buffer = new byte[1];
            return Buffer;
        }

        /// <summary>
        /// 获取或设置当前文件名筛选器字符串，该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容
        /// </summary>
        public string Filter
        {
            get
            {
                return openFileDialog1.Filter;
            }
            set
            {
                openFileDialog1.Filter = value;
            }
        }

        private void txtFileName_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (FileName.Length > 0)
                {
                    FileInfo EleFile = new FileInfo(FileName);
                    if (EleFile.Exists)
                    {
                        System.Diagnostics.Process.Start(FileName);
                    }
                }
                else if (Buffer.Length > 1 && FileNameControl != null)
                {
                    string fname = Path.GetTempPath() + FileNameControl.Text;
                    File.WriteAllBytes(fname, Buffer);
                    System.Diagnostics.Process.Start(fname);
                }
            }
            catch (Exception ex)
            {
                Funcs.ShowError(ex);
            }
        }
    }
}
